`:top
In `F33f`_`[computer graphics`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Computer_graphics]`_`f, the `!Cohen–Sutherland algorithm`! is an `F33f`_`[algorithm`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Algorithm]`_`f used for `F33f`_`[line clipping`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Line_clipping]`_`f. The algorithm divides a two-dimensional space into 9 regions and then efficiently determines the lines and portions of lines that are visible in the central region of interest (the `F33f`_`[viewport`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Viewport]`_`f).
The algorithm was developed in 1967 during `F33f`_`[flight simulator`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Flight_simulator]`_`f work by `F33f`_`[Danny Cohen`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Danny_Cohen_(engineer)]`_`f and `F33f`_`[Ivan Sutherland`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Ivan_Sutherland]`_`f.`:cite-ref-sproull-1-0[`F5bf`_`[1`#cite-note-sproull-1]`_`f]
>>Contents
• `F0af`_`[The algorithm`#the-algorithm]`_`f
• `F0af`_`[Example C/C++ implementation`#example-c-c-implementation]`_`f
• `F0af`_`[Notes`#notes]`_`f
• `F0af`_`[See also`#see-also]`_`f
• `F0af`_`[References`#references]`_`f
• `F0af`_`[External links`#external-links]`_`f
-─
>>The algorithm
The algorithm includes, excludes or partially includes the line based on whether:
• Both endpoints are in the viewport region (bitwise OR of endpoints = 0000): `F33f`_`[trivial accept`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Trivial_accept]`_`f.
• Both endpoints share at least one non-visible region, which implies that the line does not cross the visible region. (bitwise AND of endpoints ≠ 0000): `F33f`_`[trivial reject`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Trivial_reject]`_`f.
• Both endpoints are in different regions: in case of this nontrivial situation the algorithm finds one of the two points that is outside the viewport region (there will be at least one point outside). The intersection of the outpoint and extended viewport border is then calculated (i.e. with the parametric equation for the line), and this new point replaces the outpoint. The algorithm repeats until a trivial accept or reject occurs.
The numbers in the figure below are called `F33f`_`[outcodes`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Outcodes_(computer_graphics)]`_`f. An outcode is computed for each of the two points in the line. The outcode will have 4 bits for two-dimensional clipping, or 6 bits in the three-dimensional case. The first bit is set to 1 if the point is above the viewport. The bits in the 2D outcode represent: top, bottom, right, left. For example, the outcode 1010 represents a point that is top-right of the viewport.
left central right top 1001 1000 1010 central 0001 0000 0010 bottom 0101 0100 0110
Note that the outcodes for endpoints `*must`* be recalculated on each iteration after the clipping occurs.
The Cohen–Sutherland algorithm can be used only on a rectangular `F33f`_`[clip window`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Clip_window]`_`f.
>>Example C/C++ implementation
`B100`F9d9typedef int OutCode;`f`b
`B100`F9d9`f`b
`B100`F9d9const int INSIDE = 0b0000;`f`b
`B100`F9d9const int LEFT = 0b0001;`f`b
`B100`F9d9const int RIGHT = 0b0010;`f`b
`B100`F9d9const int BOTTOM = 0b0100;`f`b
`B100`F9d9const int TOP = 0b1000;`f`b
`B100`F9d9`f`b
`B100`F9d9// Compute the bit code for a point (x, y) using the clip rectangle`f`b
`B100`F9d9// bounded diagonally by (xmin, ymin), and (xmax, ymax)`f`b
`B100`F9d9`f`b
`B100`F9d9// ASSUME THAT xmax, xmin, ymax and ymin are global constants.`f`b
`B100`F9d9`f`b
`B100`F9d9OutCode ComputeOutCode(double x, double y)`f`b
`B100`F9d9{`f`b
`B100`F9d9 OutCode code = INSIDE; // initialised as being inside of clip window`f`b
`B100`F9d9`f`b
`B100`F9d9 if (x < xmin) // to the left of clip window`f`b
`B100`F9d9 code |= LEFT;`f`b
`B100`F9d9 else if (x > xmax) // to the right of clip window`f`b
`B100`F9d9 code |= RIGHT;`f`b
`B100`F9d9 if (y < ymin) // below the clip window`f`b
`B100`F9d9 code |= BOTTOM;`f`b
`B100`F9d9 else if (y > ymax) // above the clip window`f`b
`B100`F9d9 code |= TOP;`f`b
`B100`F9d9`f`b
`B100`F9d9 return code;`f`b
`B100`F9d9}`f`b
`B100`F9d9`f`b
`B100`F9d9// Cohen–Sutherland clipping algorithm clips a line from`f`b
`B100`F9d9// P0 = (x0, y0) to P1 = (x1, y1) against a rectangle with`f`b
`B100`F9d9// diagonal from (xmin, ymin) to (xmax, ymax).`f`b
`B100`F9d9bool CohenSutherlandLineClip(double& x0, double& y0, double& x1, double& y1)`f`b
`B100`F9d9{`f`b
`B100`F9d9 // compute outcodes for P0, P1, and whatever point lies outside the clip rectangle`f`b
`B100`F9d9 OutCode outcode0 = ComputeOutCode(x0, y0);`f`b
`B100`F9d9 OutCode outcode1 = ComputeOutCode(x1, y1);`f`b
`B100`F9d9 bool accept = false;`f`b
`B100`F9d9`f`b
`B100`F9d9 while (true) {`f`b
`B100`F9d9 if (!(outcode0 | outcode1)) {`f`b
`B100`F9d9 // bitwise OR is 0: both points inside window; trivially accept and exit loop`f`b
`B100`F9d9 accept = true;`f`b
`B100`F9d9 break;`f`b
`B100`F9d9 } else if (outcode0 & outcode1) {`f`b
`B100`F9d9 // bitwise AND is not 0: both points share an outside zone (LEFT, RIGHT, TOP,`f`b
`B100`F9d9 // or BOTTOM), so both must be outside window; exit loop (accept is false)`f`b
`B100`F9d9 break;`f`b
`B100`F9d9 } else {`f`b
`B100`F9d9 // failed both tests, so calculate the line segment to clip`f`b
`B100`F9d9 // from an outside point to an intersection with clip edge`f`b
`B100`F9d9 double x, y;`f`b
`B100`F9d9`f`b
`B100`F9d9 // At least one endpoint is outside the clip rectangle; pick it.`f`b
`B100`F9d9 OutCode outcodeOut = outcode1 > outcode0 ? outcode1 : outcode0;`f`b
`B100`F9d9`f`b
`B100`F9d9 // Now find the intersection point;`f`b
`B100`F9d9 // use formulas:`f`b
`B100`F9d9 // slope = (y1 - y0) / (x1 - x0)`f`b
`B100`F9d9 // x = x0 + (1 / slope) * (ym - y0), where ym is ymin or ymax`f`b
`B100`F9d9 // y = y0 + slope * (xm - x0), where xm is xmin or xmax`f`b
`B100`F9d9 // No need to worry about divide-by-zero because, in each case, the`f`b
`B100`F9d9 // outcode bit being tested guarantees the denominator is non-zero`f`b
`B100`F9d9 if (outcodeOut & TOP) { // point is above the clip window`f`b
`B100`F9d9 x = x0 + (x1 - x0) * (ymax - y0) / (y1 - y0);`f`b
`B100`F9d9 y = ymax;`f`b
`B100`F9d9 } else if (outcodeOut & BOTTOM) { // point is below the clip window`f`b
`B100`F9d9 x = x0 + (x1 - x0) * (ymin - y0) / (y1 - y0);`f`b
`B100`F9d9 y = ymin;`f`b
`B100`F9d9 } else if (outcodeOut & RIGHT) { // point is to the right of clip window`f`b
`B100`F9d9 y = y0 + (y1 - y0) * (xmax - x0) / (x1 - x0);`f`b
`B100`F9d9 x = xmax;`f`b
`B100`F9d9 } else if (outcodeOut & LEFT) { // point is to the left of clip window`f`b
`B100`F9d9 y = y0 + (y1 - y0) * (xmin - x0) / (x1 - x0);`f`b
`B100`F9d9 x = xmin;`f`b
`B100`F9d9 }`f`b
`B100`F9d9`f`b
`B100`F9d9 // Now we move outside point to intersection point to clip`f`b
`B100`F9d9 // and get ready for next pass.`f`b
`B100`F9d9 if (outcodeOut == outcode0) {`f`b
`B100`F9d9 x0 = x;`f`b
`B100`F9d9 y0 = y;`f`b
`B100`F9d9 outcode0 = ComputeOutCode(x0, y0);`f`b
`B100`F9d9 } else {`f`b
`B100`F9d9 x1 = x;`f`b
`B100`F9d9 y1 = y;`f`b
`B100`F9d9 outcode1 = ComputeOutCode(x1, y1);`f`b
`B100`F9d9 }`f`b
`B100`F9d9 }`f`b
`B100`F9d9 }`f`b
`B100`F9d9 return accept;`f`b
`B100`F9d9}`f`b
>>Notes
`:cite-note-sproull-1`!1.`! `F0af`_`[↑`#cite-ref-sproull-1-0]`_`f `*Principles of Interactive Computer Graphics`*, p. 124, 252, by `F33f`_`[Bob Sproull`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Bob_Sproull]`_`f and William M. Newman, 1973, McGraw–Hill Education, International edition, `F33f`_`[ISBN`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=ISBN_(identifier)]`_`f 0-07-085535-8.
>>See also
Algorithms used for the same purpose:
• `F33f`_`[Liang–Barsky algorithm`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Liang–Barsky_algorithm]`_`f
• `F33f`_`[Cyrus–Beck algorithm`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Cyrus–Beck_algorithm]`_`f
• `F33f`_`[Nicholl–Lee–Nicholl algorithm`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Nicholl–Lee–Nicholl_algorithm]`_`f
• `F33f`_`[Fast clipping`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Fast_clipping]`_`f
>>References
• James D. Foley. `*Computer graphics: principles and practice`*. Addison-Wesley Professional, 1996. p. 113.
>>External links
• JavaScript polyline clipping library using Cohen-Sutherland algorithm
• Animated JavaScript implementation
• Delphi implementation
• Stata implementation
`c`F0af`_`[↑ Back to top`#top]`_`f`a